﻿using System;
using ICSharpCode.Decompiler.ILAst;
using TK_CSLua.ILAnalyze;

namespace CSLua
{
	// Token: 0x02000022 RID: 34
	public class LuaMethodGenerator : MethodGenerator
	{
		// Token: 0x06000145 RID: 325 RVA: 0x00003CC2 File Offset: 0x00001EC2
		public LuaMethodGenerator(BuildFilePhase phaseContext, LuaMethod luaMethod) : base(phaseContext, luaMethod)
		{
		}

		// Token: 0x06000146 RID: 326 RVA: 0x00008C87 File Offset: 0x00006E87
		public override ExpressionBuilder CreateExpressionBuilder(ILExpression expression)
		{
			return new LuaExpressionBuilder(this, expression);
		}

		// Token: 0x06000147 RID: 327 RVA: 0x00008C90 File Offset: 0x00006E90
		protected override void writeForeach(ILWhileLoop loop, ILVariable currVar, ILVariable iterVar)
		{
			string key = "_";
			string value = this.luaMethod.getLocalValueName(currVar);
			string iterFunc = "CSLEach";
			string iterVarName = this.luaMethod.getLocalValueName(iterVar);
			this.writer.codeFormat("for {0},{1} in {2}({3}) do", new object[]
			{
				key,
				value,
				iterFunc,
				iterVarName
			}).shiftRightNewLine();
			base.generateNode(loop.BodyBlock);
			this.writer.shiftLeftNewLine("end");
		}

		// Token: 0x06000148 RID: 328 RVA: 0x00008D0C File Offset: 0x00006F0C
		protected override void writeWhileLoop(ILWhileLoop loop)
		{
			this.writer.code("while ");
			if (loop.Condition != null)
			{
				ExpressionBuilder.buildExpItem(this, loop.Condition);
			}
			else
			{
				this.writer.code(" true");
			}
			this.writer.code(" do").shiftRightNewLine();
			base.generateNode(loop.BodyBlock);
			this.writer.shiftLeftNewLine("end");
		}

		// Token: 0x06000149 RID: 329 RVA: 0x00008D84 File Offset: 0x00006F84
		protected override void writeCondition(ILCondition segment)
		{
			this.writer.code("if ");
			ExpressionBuilder.buildExpItem(this, segment.Condition);
			this.writer.code(" then").shiftRightNewLine();
			base.generateNode(segment.TrueBlock);
			if (segment.FalseBlock == null || segment.FalseBlock.Body.Count == 0)
			{
				this.writer.shiftLeftNewLine("end");
				return;
			}
			this.writer.shiftLeftNewLine("else").shiftRightNewLine();
			base.generateNode(segment.FalseBlock);
			this.writer.shiftLeftNewLine("end");
		}
	}
}
